home *** CD-ROM | disk | FTP | other *** search
/ Aminet 20 / Aminet 20 (1997)(GTI - Schatztruhe)[!][Aug 1997].iso / Aminet / dev / src / DICE_SharedLib.lha / Memory.c < prev    next >
C/C++ Source or Header  |  1997-06-23  |  3KB  |  146 lines

  1. //
  2. //        Example Shared Library Code
  3. //        Compiles with DICE
  4. //        
  5. //        By Wez Furlong <wez@twinklestar.demon.co.uk>
  6. //
  7. //        Based on code by Geert Uytterhoeven and Matt Dillon
  8. //
  9. //        This source was produced:    Monday 23-Jun-1997 
  10. //
  11. //        DISCLAIMER
  12. //
  13. //        Please read the code FULLY before use... I could have put ANYTHING in
  14. //        here; I may have the code format your bootdrive for example.
  15. //
  16. //        NEVER trust example code without fully understanding what it does.
  17. //
  18. //        This code comes with no warranty; I am NOT responsible for any damage
  19. //        that may ensue from its use, be it physical, mental or otherwise.
  20. //
  21. //        This code may be modified, so long as the names of myself, Geert and
  22. //        Matt are mentioned within any release or distribution produced using it,
  23. //        and a copy sent to myself.
  24. //
  25. //        This code may be redistributed freely; no profit is allowed to be made
  26. //        from its distribution.
  27. //
  28. //        This code may be included on an Aminet or Fred Fish CD.
  29. //
  30.  
  31. //--------- Memory handling: provides memory pools for the library, making it
  32. //---------    more memory efficient.
  33.  
  34. #include "example.h"
  35.  
  36. //--    Function Prototypes
  37.  
  38. //--    Use these routines for memory allocation
  39.  
  40. Prototype APTR MAlloc(ULONG size);
  41. Prototype void Free(APTR block, ULONG size);
  42. Prototype APTR MAllocV(ULONG size);
  43. Prototype void FreeV(APTR block);
  44.  
  45. Local BOOL InitMemory(void);
  46. Local void CleanUpMemory(void);
  47.  
  48. //-- Should fit in a 4K memory page
  49.  
  50. #define MEM_PUDDLESIZE    4000
  51. #define MEM_THRESHSIZE    4000
  52.  
  53. //-- Note: substitute for LibAllocPooled etc. if your target is <3.0
  54.  
  55. APTR CreatePool(ULONG, ULONG, ULONG);
  56. void DeletePool(APTR);
  57. APTR AllocPooled(APTR, ULONG);
  58. void FreePooled(APTR, APTR, ULONG);
  59.  
  60. //--    Our Private Memory Pool
  61.  
  62. APTR Pool = NULL;
  63.  
  64. //--    Access Control Semaphore
  65.  
  66. struct SignalSemaphore Semaphore;
  67.  
  68. //--    Initialisation
  69.  
  70. BOOL InitMemory(void)
  71. {
  72.     InitSemaphore(&Semaphore);
  73.     ObtainSemaphore(&Semaphore);
  74.     Pool = CreatePool(MEMF_PUBLIC, MEM_PUDDLESIZE, MEM_THRESHSIZE);
  75.     ReleaseSemaphore(&Semaphore);
  76.     return((BOOL)(Pool ? TRUE : FALSE));
  77. }
  78.  
  79.  
  80. //--    Clean Up
  81.  
  82. void CleanUpMemory(void)
  83. {
  84.     ObtainSemaphore(&Semaphore);
  85.     if (Pool) {
  86.         DeletePool(Pool);
  87.         Pool = NULL;
  88.     }
  89.     ReleaseSemaphore(&Semaphore);
  90. }
  91.  
  92.  
  93. //--    Replacement for AllocMem()
  94.  
  95. APTR MAlloc(ULONG size)
  96. {
  97.     ULONG *block;
  98.  
  99.     ObtainSemaphore(&Semaphore);
  100.     if (block = AllocPooled(Pool, size))
  101.         memset(block, NULL, size);
  102.     ReleaseSemaphore(&Semaphore);
  103.     return(block);
  104. }
  105.  
  106.  
  107. //--    Replacement for FreeMem()
  108.  
  109. void Free(APTR block, ULONG size)
  110. {
  111.     if (block) {
  112.         ObtainSemaphore(&Semaphore);
  113.         FreePooled(Pool, block, size);
  114.         ReleaseSemaphore(&Semaphore);
  115.     }
  116. }
  117.  
  118.  
  119. //--    Replacement for AllocVec()
  120.  
  121. APTR MAllocV(ULONG size)
  122. {
  123.     ULONG *block;
  124.  
  125.     ObtainSemaphore(&Semaphore);
  126.     if (block = AllocPooled(Pool, size+4)) {
  127.         *(block++) = size;
  128.         memset(block, NULL, size);
  129.     }
  130.     ReleaseSemaphore(&Semaphore);
  131.     return(block);
  132. }
  133.  
  134.  
  135. //--    Replacement for FreeVec()
  136.  
  137. void FreeV(APTR block)
  138. {
  139.     if (block) {
  140.         ObtainSemaphore(&Semaphore);
  141.         FreePooled(Pool, (APTR)((ULONG)block-4), *(ULONG *)((ULONG)block-4));
  142.         ReleaseSemaphore(&Semaphore);
  143.     }
  144. }
  145.  
  146.